Migrated TranslationAgent to liquid

Dominik Sander 11 years ago
parent
commit
39360929df

+ 6 - 5
app/models/agents/translation_agent.rb

@@ -1,5 +1,6 @@
1 1
 module Agents
2 2
   class TranslationAgent < Agent
3
+    include LiquidInterpolatable
3 4
 
4 5
     cannot_be_scheduled!
5 6
 
@@ -8,7 +9,7 @@ module Agents
8 9
       Services are provided using Microsoft Translator. You can [sign up](https://datamarket.azure.com/dataset/bing/microsofttranslator) and [register your application](https://datamarket.azure.com/developer/applications/register) to get `client_id` and `client_secret` which are required to use this agent.
9 10
       `to` must be filled with a [translator language code](http://msdn.microsoft.com/en-us/library/hh456380.aspx).
10 11
 
11
-      Specify what you would like to translate in `content` field, by specifying key and JSONPath of content to be translated.
12
+      Specify what you would like to translate in `content` field, you can use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) specify which part of the payload you want to translate.
12 13
 
13 14
       `expected_receive_period_in_days` is the maximum number of days you would allow to pass between events.
14 15
     MD
@@ -22,8 +23,8 @@ module Agents
22 23
         'to' => "fi",
23 24
         'expected_receive_period_in_days' => 1,
24 25
         'content' => {
25
-          'text' => "$.message.text",
26
-          'content' => "$.xyz"
26
+          'text' => "{{message.text}}",
27
+          'content' => "{{xyz}}"
27 28
         }
28 29
       }
29 30
     end
@@ -68,8 +69,8 @@ module Agents
68 69
       incoming_events.each do |event|
69 70
         translated_event = {}
70 71
         options['content'].each_pair do |key, value|
71
-          to_be_translated = Utils.values_at event.payload, value
72
-          translated_event[key] = translate to_be_translated.first, options['to'], access_token
72
+          to_be_translated = interpolate_string(value, event.payload)
73
+          translated_event[key] = translate(to_be_translated.first, options['to'], access_token)
73 74
         end
74 75
         create_event :payload => translated_event
75 76
       end

+ 8 - 0
db/migrate/20140501200859_migrate_translation_agent_to_liquid.rb

@@ -0,0 +1,8 @@
1
+class MigrateTranslationAgentToLiquid < ActiveRecord::Migration
2
+  def change
3
+    Agent.where(:type => 'Agents::TranslationAgent').each do |agent|
4
+      agent.options['content'] = LiquidMigrator.convert_hash(agent.options['content'], {:merge_path_attributes => true, :leading_dollarsign_is_jsonpath => true})
5
+      agent.save
6
+    end
7
+  end
8
+end

+ 6 - 2
spec/models/agents/translation_agent_spec.rb

@@ -1,6 +1,10 @@
1 1
 require 'spec_helper'
2
+require 'models/concerns/liquid_interpolatable'
3
+
2 4
 
3 5
 describe Agents::TranslationAgent do
6
+    it_behaves_like LiquidInterpolatable
7
+
4 8
     before do
5 9
         @valid_params = {
6 10
             :name    => "somename",
@@ -10,8 +14,8 @@ describe Agents::TranslationAgent do
10 14
                 :to            => "fi",
11 15
                 :expected_receive_period_in_days => 1,
12 16
                 :content       => {
13
-                    :text => "$.message",
14
-                    :content => "$.xyz"
17
+                    :text => "{{message}}",
18
+                    :content => "{{xyz}}"
15 19
                 }
16 20
             }
17 21
         }